package name.marz.to.go.ahg.idemitsu;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.nodes.Document;

import name.marz.to.go.ahg.common.util.HttpUtil;
import name.marz.to.go.ahg.core.AutoGetterHttpClient;
import name.marz.to.go.ahg.core.Parameters;
import name.marz.to.go.ahg.core.checker.DocumentChecker;
import name.marz.to.go.ahg.core.executer.AbstractLoginExecuter;

public class IdemitsuLoginExecuter extends AbstractLoginExecuter {
	private static final String FORM_NAME = "_USA01Form";
	private DocumentChecker checker = new IdemitsuLoginChecker();
	private Document doc;
	
	public IdemitsuLoginExecuter(URI baseUri, AutoGetterHttpClient client,String[] userIds, String pass, Document doc) {
		super(baseUri, client, userIds, pass);
		this.doc = doc;
		// URIをactionで再構築
		String action = HttpUtil.getFormActionString(doc, FORM_NAME);
		super.uri = HttpUtil.createURISafety(baseUri.getScheme(), baseUri.getAuthority(), action);
	}

	@Override
	protected List<NameValuePair> getRequestParameters() {
		Parameters p = HttpUtil.getParametersFromFormTag(doc, FORM_NAME);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("inputId", userIdentifiers[0]));
		params.add(new BasicNameValuePair("inputPassword", userPass));
		params.add(p.getNameValuePair("event"));
		params.add(p.getNameValuePair("org.apache.struts.taglib.html.TOKEN"));
		params.add(p.getNameValuePair("login"));
		return params;
	}

	@Override
	protected DocumentChecker getChecker() {
		return checker;
	}
}
